package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.StorageMapper;
import com.gxa.zhonglong.pojo.Storage;
import com.gxa.zhonglong.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @auther JJuventus
 * @date 2021/10/29 14:58
 */

@Service("StorageService")
public class StorageServiceImpl implements StorageService {

    @Autowired
    private StorageMapper storageMapper = null;

    @Override
    public List<Storage> select(String storageJewelryCode, String storageJewelryName, String storageStore, Integer page, Integer size) {
        Integer offside = (page-1)*size;
        List<Storage> storages = storageMapper.select(storageJewelryCode, storageJewelryName, storageStore, offside, size);
        return storages;
    }

    @Override
    public List<Storage> selectAll(String storageJewelryCode, String storageJewelryName, String storageStore) {
        return storageMapper.selectAll(storageJewelryCode, storageJewelryName, storageStore);
    }

    @Override
    public void update(Storage storage) throws BusinessException {
        Storage storage1 = storageMapper.selectById(storage.getStorageId());
        if (storage1!=null && storage1.getStorageCount()>0){
            storageMapper.update(storage);
        }else{
            throw new BusinessException(ExceptionCode.NO_STORAGE);
        }
    }

    @Override
    public Integer add(Storage storage) {
        storage.setStorageWarehousingTime(new Timestamp(System.currentTimeMillis()));
        Integer add = storageMapper.add(storage);
        return add;
    }

    @Override
    public Double selectByWeight(String storageId) {
        return storageMapper.selectByWeight(storageId);
    }

    @Override
    public Double selectByCost(String storageId) {
        return storageMapper.selectByCost(storageId);
    }

    @Override
    public Integer selectByCount(String storageId) {
        return storageMapper.selectByCount(storageId);
    }

    @Override
    public void delete(String num) {
        storageMapper.delete(num);
    }

    @Override
    public void updateSAC(String shop, String counter, String num) {
        storageMapper.updateSAC(shop,counter,num);
    }
}
